﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.SinhVienTrungTuyenService;
namespace eTMS.Controllers
{
    public class SinhVienTrungTuyenController : SinhVienTrungTuyenControllerBase
    {
        public SinhVienTrungTuyenController()
            : base()
        {
        }
        public DataTable LoadAllDSInListSBD(List<string> lsSBD, int KhoaHoc_ID)
        {
            if (lsSBD.Count == 0) return new DataTable();
            List<string> ls = new List<string>();
            DataTable result = null;
            for (int i = 0; i < lsSBD.Count; i++)
            {
                ls.Add("'" + lsSBD[i].Trim().Replace("'", "\'") + "'");
                if (ls.Count > 1000)
                {
                    DataTable tbl = dao.SelectAllDSFilter("SoBaoDanh IN(" + string.Join(",", ls.ToArray()) + ") AND KhoaHoc_ID=" + KhoaHoc_ID.ToString() + " AND HeDaoTao_ID=" + CommonController.HeDaoTao.HeDaoTao_ID.ToString()).Tables[0];
                    if (result == null)
                    {
                        result = tbl;
                    }
                    else
                    {
                        foreach (DataRow row in tbl.Rows)
                        {
                            result.ImportRow(row);
                        }
                    }
                    ls = new List<string>();
                }
            }
            if (ls.Count > 0)
            {
                DataTable tbl = dao.SelectAllDSFilter("SoBaoDanh IN(" + string.Join(",", ls.ToArray()) + ") AND KhoaHoc_ID=" + KhoaHoc_ID.ToString() + " AND HeDaoTao_ID=" + CommonController.HeDaoTao.HeDaoTao_ID.ToString()).Tables[0];
                if (result == null)
                {
                    result = tbl;
                }
                else
                {
                    foreach (DataRow row in tbl.Rows)
                    {
                        result.ImportRow(row);
                    }
                }
            }
            return result;
        }

        public bool SaveList(List<SinhVienTrungTuyenEntity> list)
        {
            try
            {
                List<SinhVienTrungTuyenEntity> lsTmp = new List<SinhVienTrungTuyenEntity>();
                for (int i = 0; i < list.Count; i++)
                {
                    lsTmp.Add(list[i]);
                    if (lsTmp.Count > 10)
                    {
                        dao.SaveList(lsTmp.ToArray());
                        lsTmp=new List<SinhVienTrungTuyenEntity>();
                    }

                }
                if(lsTmp.Count>0)
                    dao.SaveList(lsTmp.ToArray());
            }
            catch
            {
                return false;
            }
            return true;
        }
        public void LoadAllDSDataFilter(int KhoaHoc_ID, string Filter)
        {
            if (Filter.Length > 0)
            {
                Table = dao.SelectAllDSFilter(Filter +" AND KhoaHoc_ID="+ KhoaHoc_ID.ToString()).Tables[0];
            }
            else
            {
                Table = dao.SelectAllDSFilter("KhoaHoc_ID="+KhoaHoc_ID.ToString()).Tables[0];
            }
            AddIndexToTable(); 
        }
    }
}
